package com.bestcem.xm.user.controller.param;

import com.bestcem.xm.common.core.domain.web.XmViewResultJson;
import com.bestcem.xm.common.core.enums.ReqErrorCodeEnum;
import com.bestcem.xm.user.constant.CommonConstant;
import com.bestcem.xm.user.controller.vo.UpdateUnsubscribeSettingVO;
import com.bestcem.xm.user.enums.UnsubscribeTypeEnum;
import lombok.Data;

import javax.validation.constraints.NotNull;
import java.util.Objects;

/**
 * 修改退订设置参数
 *
 * @author ympan <yuming.pan@idiaoyan.com>
 * @version v1.0
 * @date 2022/7/12 14:16
 */
@Data
public class UpdateUnsubscribeSettingParam {
    /**
     * 主键
     */
    private String id;

    /**
     * 公司id
     */
    private String orgId;

    /**
     * 退订类型，1:期限退订，2:永久退订
     *
     * @see UnsubscribeTypeEnum
     */
    @NotNull(message = "退订类型不能为空")
    private Integer type;

    /**
     * 持续时间，单位天，仅期限退订时设置
     */
    private Integer duration;

    /**
     * 用户id
     */
    private String userId;

    /**
     * 参数校验
     *
     * @return 成功与否
     * @author ympan <yuming.pan@idiaoyan.com>
     * @date 10:44 2022/8/4
     */
    public XmViewResultJson<UpdateUnsubscribeSettingVO> valid() {
        if (Objects.isNull(UnsubscribeTypeEnum.getByType(this.getType()))) {
            return XmViewResultJson.fail(ReqErrorCodeEnum.PARAM_FORMAT.getCode(), "退订类型超出范围");
        }

        if (UnsubscribeTypeEnum.SPECIFIED.getType().equals(type) && Objects.isNull(this.getDuration())) {
            return XmViewResultJson.fail(ReqErrorCodeEnum.PARAM_FORMAT.getCode(), "持续时间不能为空");
        }
        if (UnsubscribeTypeEnum.SPECIFIED.getType().equals(type) && (this.getDuration() < CommonConstant.SPECIFIED_DURATION_MIN || this.getDuration() > CommonConstant.SPECIFIED_DURATION_MAX)) {
            return XmViewResultJson.fail(ReqErrorCodeEnum.PARAM_FORMAT.getCode(), "持续时间超出范围");
        }
        return XmViewResultJson.success();
    }
}
    